package org.cbg.panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.cbg.btn.CBGDahuabiBuyBtn;
import org.cbg.btn.TrslationBtn;
import org.cbg.entity.Collection;
import org.cbg.entity.Salegoods;
import org.cbg.frame.TraslationCommodityJFrame;
import org.cbg.frame.TrslationMainJframe;
import org.cbg.mouslisten.CBGmoveMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;

import com.tool.tcpimg.UIUtils;

 

/**
 * 我要买大话币模型面板
 * 
 * @author Administrator
 * 
 */

public class TraslationWantBuyDaojuModelJpanel extends JPanel {
    // 展示底框 展示图标 大话币上 大话币下 价格 单价
    private JLabel zhanshidikuang, tupian, dahuabixia, danjia, price;
    // 收藏按钮
    private TrslationBtn shoucang;

    // 购买button
    private CBGDahuabiBuyBtn buy;

    /**
     * 是否被收藏标识(0表示被收藏，1表示没有被收藏) 公示期标识(0 在公示期 ， 1上架)
     */
    private int stateOrNo = 1, gongshiqiBiaoshi = 0;

    public TraslationWantBuyDaojuModelJpanel() {
        this.setLayout(null);
        this.setPreferredSize(new Dimension(740, 62));
        this.setOpaque(false);

        shoucang = new TrslationBtn("inkImg/number/b4/qxscxx.png", 1);
        tupian = new JLabel();
        zhanshidikuang = new JLabel();
        dahuabixia = new JLabel("四千八百万");
        danjia = new JLabel("48.6万两/元");

        zhanshidikuang.setBorder(null);
        zhanshidikuang.setBounds(38, 2, 53, 53);
        zhanshidikuang.setIcon(CutButtonImage.getImage("inkImg/number/b4/spbj.png", 53, 53));
        zhanshidikuang.setOpaque(false);
        this.add(zhanshidikuang);

        price = new JLabel("￥100.0");
        dahuabixia.setFont(UIUtils.TEXT_FONT1);
        dahuabixia.setForeground(UIUtils.COLOR_cbg3);
        //        dahuabixia.setHorizontalAlignment(SwingConstants.CENTER);
        danjia.setFont(UIUtils.TEXT_FONT2);
        danjia.setForeground(UIUtils.COLOR_cbg6);
        price.setFont(UIUtils.TEXT_FONT60);
        price.setForeground(UIUtils.COLOR_cbg4);

        shoucang.setBounds(5, 15, 24, 27);
        tupian.setBounds(40, 5, 48, 48);
        dahuabixia.setBounds(100, 8, 135, 24);
        price.setBounds(200, 12, 125, 40);
        danjia.setBounds(360, 12, 125, 40);

        buy = new CBGDahuabiBuyBtn("inkImg/number/b4/kbaj.png", 1, UIUtils.COLOR_BTNPUTONG1, UIUtils.TEXT_HY166, 2, "预购", this);
        buy.setBounds(680, 15, 55, 27);

        this.add(buy);
        this.add(shoucang);
        this.add(tupian);
        this.add(dahuabixia);
        this.add(danjia);
        this.add(price);
        // 是否被收藏
        shoucang.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (e.getButton() == MouseEvent.BUTTON1) {
                    try {
                        if (stateOrNo == 1) {
                            shoucang.setIcons(CutButtonImage.cuts("inkImg/number/b4/scxx.png"));
                            stateOrNo = 0;
                        } else {
                            shoucang.setIcons(CutButtonImage.cuts("inkImg/number/b4/qxscxx.png"));
                            stateOrNo = 1;
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }

            }
        });

    }

    private Salegoods salegoods;

    /**
     * 将商品信息整合到界面
     *
     * @param salegoods
     */
    public TraslationWantBuyDaojuModelJpanel(Salegoods salegoods) {
        this.setLayout(null);
        this.setPreferredSize(new Dimension(740, 62));
        this.setOpaque(false);
        this.salegoods = salegoods;
        shoucang = new TrslationBtn("inkImg/number/b4/qxscxx.png", 1);
        tupian = new JLabel();
        zhanshidikuang = new JLabel();
        dahuabixia = new JLabel(salegoods.getSalename());
        // 获取String类型的时间
        String remainingTime = TrslationMainJframe.remainingTime(salegoods.getUptime(), 1);
        gongshiqiBiaoshi = 0;
        buy = new CBGDahuabiBuyBtn("inkImg/number/b4/kbaj.png", 1, UIUtils.COLOR_BTNPUTONG1, UIUtils.TEXT_HY166, 2, "预购", this);
        if (remainingTime.equals("已下架")) {
            buy.setText("购买");
            remainingTime = TrslationMainJframe.remainingTime(salegoods.getUptime(), 168);
            gongshiqiBiaoshi = 1;
        }
        danjia = new JLabel(remainingTime);

        zhanshidikuang.setBorder(null);
        zhanshidikuang.setBounds(38, 2, 53, 53);
        zhanshidikuang.setIcon(CutButtonImage.getImage("inkImg/number/b4/spbj.png", 53, 53));
        zhanshidikuang.setOpaque(false);
        this.add(zhanshidikuang);

        price = new JLabel();
        if (salegoods.getSaleprice() != null) {
            price.setText(salegoods.getSaleprice() + "");
        }
        dahuabixia.setFont(UIUtils.TEXT_FONT1);
        dahuabixia.setForeground(UIUtils.COLOR_cbg3);
        //        dahuabixia.setHorizontalAlignment(SwingConstants.CENTER);
        danjia.setFont(UIUtils.TEXT_FONT2);
        danjia.setForeground(UIUtils.COLOR_cbg6);
        price.setFont(UIUtils.TEXT_FONT60);
        price.setForeground(UIUtils.COLOR_cbg4);

        if (salegoods.getSaleskin() != null) {
            ImageIcon cbg = CutButtonImage.getCBG(salegoods.getSaletype(), salegoods.getSaleskin(), 48, 48);
            tupian.setIcon(cbg);

        }

        shoucang.setBounds(5, 15, 24, 27);
        tupian.setBounds(40, 5, 48, 48);
        dahuabixia.setBounds(100, 8, 135, 24);
        price.setBounds(250, 12, 125, 40);
        danjia.setBounds(560, 12, 125, 40);

        /**
         * 保存角色ID，商品ID
         */
        if (salegoods.getRoleid() != null) {
            price.setName(salegoods.getRoleid().toString());
        }
        if (salegoods.getSaleid() != null) {
            tupian.setName(salegoods.getSaleid().toString());
        }
        if (salegoods.getOtherid() != null) {
            dahuabixia.setName(salegoods.getOtherid().toString());
        }
        buy.setName(salegoods.getSaletype().toString());
        /**
         * 修改名字来添加数据
         */

        if (salegoods.getBuyrole() != null) {
            danjia.setName(salegoods.getBuyrole().toString());
        }

        buy.setBounds(680, 15, 55, 27);
        buy.setBorder(null);
        buy.setOpaque(false);

        this.add(buy);
        this.add(shoucang);
        this.add(tupian);
        this.add(dahuabixia);
        this.add(danjia);
        this.add(price);
        // 是否被收藏
        shoucang.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                TrslationMainJframe.getTrslationMainJframe().setShoucangBtn(e.getSource());
                TrslationBtn sss = (TrslationBtn) TrslationMainJframe.getTrslationMainJframe().getShoucangBtn();
                if (e.getButton() == MouseEvent.BUTTON1) {
                    try {
                        if (stateOrNo == 1) {
                            sss.setIcons(CutButtonImage.cuts("inkImg/number/b4/scxx.png"));
                            stateOrNo = 0;
                        } else {
                            sss.setIcons(CutButtonImage.cuts("inkImg/number/b4/qxscxx.png"));
                            stateOrNo = 1;
                        }
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    if (tupian.getName().equals(
                            TraslationCommodityJFrame.getTraslationCommodityJFrame().getTraslationCommodityMainJPane()
                                    .getName())) {
                        TraslationCommodityJFrame.getTraslationCommodityJFrame().getTraslationCommodityMainJPane()
                                .getShoucang().setIcons(shoucang.getIcons());
                    }
                }
                /**
                 * 藏宝阁收藏-取消 1、初始化Collection对象 2、添加角色ID，商品ID 3、根据标识转为对应的json字符串
                 * 4、提交给服务器
                 */
                // Collection collection = new Collection();
                // collection.setRoleid(new BigDecimal(price.getName()));
                // collection.setSaleid(new BigDecimal(tupian.getName()));
                String sendmes = Agreement.getAgreement().searchCollectionAgreement(tupian.getName());
                SendMessageUntil.toServer(sendmes);

                /**
                 * 修正收藏物品
                 */
                if (TrslationMainJframe.getTrslationMainJframe().getShoucangWupinList()
                        .contains(new BigDecimal(tupian.getName()))) {
                    TrslationMainJframe.getTrslationMainJframe().getShoucangWupinList()
                            .remove(new BigDecimal(tupian.getName()));
                } else {
                    TrslationMainJframe.getTrslationMainJframe().getShoucangWupinList()
                            .add(new BigDecimal(tupian.getName()));
                }
            }
        });

        /**
         * 添加展示面板
         */
        tupian.addMouseListener(new CBGmoveMouslisten(salegoods.getSaletype(), salegoods.getOtherid()));
    }

    private Collection collection;

    /**
     * 将商品信息整合到界面（收藏）
     *
     * @param salegoods
     */
    public TraslationWantBuyDaojuModelJpanel(Collection collection) {
        this.setLayout(null);
        this.setPreferredSize(new Dimension(740, 62));
        this.setOpaque(false);
        this.collection = collection;
        shoucang = new TrslationBtn("inkImg/number/b4/qxscxx.png", 1);
        tupian = new JLabel();
        zhanshidikuang = new JLabel();
        dahuabixia = new JLabel(collection.getSalename());
        // 获取String类型的时间
        String remainingTime = TrslationMainJframe.remainingTime(collection.getUptime(), 1);
        gongshiqiBiaoshi = 0;
        buy = new CBGDahuabiBuyBtn("inkImg/number/b4/kbaj.png", 1, UIUtils.COLOR_BTNPUTONG1, UIUtils.TEXT_HY166, 2, "预购", this);
        if (remainingTime.equals("已下架")) {
            buy.setText("购买");
            remainingTime = TrslationMainJframe.remainingTime(collection.getUptime(), 168);
            gongshiqiBiaoshi = 1;
        }
        danjia = new JLabel(remainingTime);//

        zhanshidikuang.setBorder(null);
        zhanshidikuang.setBounds(38, 2, 53, 53);
        zhanshidikuang.setIcon(CutButtonImage.getImage("inkImg/number/b4/spbj.png", 53, 53));
        zhanshidikuang.setOpaque(false);
        this.add(zhanshidikuang);

        price = new JLabel();
        if (collection.getSaleprice() != null) {
            price.setText(collection.getSaleprice() + "");
        }
        dahuabixia.setFont(UIUtils.TEXT_FONT1);
        dahuabixia.setForeground(UIUtils.COLOR_cbg3);
        //        dahuabixia.setHorizontalAlignment(SwingConstants.CENTER);
        danjia.setFont(UIUtils.TEXT_FONT2);
        danjia.setForeground(UIUtils.COLOR_cbg6);
        price.setFont(UIUtils.TEXT_FONT60);
        price.setForeground(UIUtils.COLOR_cbg4);

        // 获取图片
        if (collection.getSaleskin() != null) {
            ImageIcon cbg = CutButtonImage.getCBG(collection.getSaletype(), collection.getSaleskin(), 48, 48);
            // cbg.setImage(cbg.getImage().getScaledInstance(39, 39,
            // Image.SCALE_DEFAULT));
            tupian.setIcon(cbg);

        }
        try {
            shoucang.setIcons(CutButtonImage.cuts("inkImg/number/b4/scxx.png"));
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        stateOrNo = 0;
        shoucang.setBounds(5, 15, 24, 27);
        tupian.setBounds(40, 5, 48, 48);
        dahuabixia.setBounds(100, 8, 135, 24);
        price.setBounds(250, 12, 125, 40);
        danjia.setBounds(560, 12, 125, 40);

        /**
         * 保存角色ID，商品ID
         */
        if (collection.getRoleid() != null) {
            price.setName(collection.getRoleid().toString());
        }
        if (collection.getSaleid() != null) {
            tupian.setName(collection.getSaleid().toString());
        }
        if (collection.getColid() != null) {
            dahuabixia.setName(collection.getColid().toString());
        }
        buy.setName(collection.getColid() + "");

        buy.setBounds(680, 15, 55, 27);
        buy.setBorder(null);
        buy.setOpaque(false);

        this.add(buy);
        this.add(shoucang);
        this.add(tupian);
        this.add(dahuabixia);
        this.add(danjia);
        this.add(price);
        // 是否被收藏
        shoucang.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                TrslationMainJframe.getTrslationMainJframe().setShoucangBtn(e.getSource());
                TrslationBtn sss = (TrslationBtn) TrslationMainJframe.getTrslationMainJframe().getShoucangBtn();
                if (e.getButton() == MouseEvent.BUTTON1) {
                    try {
                        if (stateOrNo == 1) {
                            sss.setIcons(CutButtonImage.cuts("inkImg/number/b4/scxx.png"));

                            stateOrNo = 0;
                        } else {
                            sss.setIcons(CutButtonImage.cuts("inkImg/number/b4/qxscxx.png"));
                            stateOrNo = 1;
                        }
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    if (tupian.getName().equals(
                            TraslationCommodityJFrame.getTraslationCommodityJFrame().getTraslationCommodityMainJPane()
                                    .getName())) {
                        TraslationCommodityJFrame.getTraslationCommodityJFrame().getTraslationCommodityMainJPane()
                                .getShoucang().setIcons(shoucang.getIcons());
                    }
                }
                /**
                 * 藏宝阁收藏-取消 1、初始化Collection对象 2、添加角色ID，商品ID 3、根据标识转为对应的json字符串
                 * 4、提交给服务器
                 */
                // Collection collection = new Collection();
                // collection.setRoleid(new BigDecimal(price.getName()));
                // collection.setSaleid(new BigDecimal(tupian.getName()));
                String sendmes = Agreement.getAgreement().searchCollectionAgreement(tupian.getName());
                SendMessageUntil.toServer(sendmes);

            }
        });

        /**
         * 添加展示面板
         */
        tupian.addMouseListener(new CBGmoveMouslisten(collection.getSaletype(), collection.getOtherid()));
    }

    private ImageIcon icon1;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
         g.setColor(new Color(245, 245, 245));
        if (icon1 == null) {
            icon1 = CutButtonImage.getImage("inkImg/old/1/Z1001.png", 740, 2);

        }
        g.drawImage(icon1.getImage(), 1, 58, 740, 2, this);

    }

    public TrslationBtn getShoucang() {
        return shoucang;
    }

    public void setShoucang(TrslationBtn shoucang) {
        this.shoucang = shoucang;
    }

    public JLabel getZhanshidikuang() {
        return zhanshidikuang;
    }

    public void setZhanshidikuang(JLabel zhanshidikuang) {
        this.zhanshidikuang = zhanshidikuang;
    }

    public JLabel getTupian() {
        return tupian;
    }

    public void setTupian(JLabel tupian) {
        this.tupian = tupian;
    }

    public JLabel getDahuabixia() {
        return dahuabixia;
    }

    public void setDahuabixia(JLabel dahuabixia) {
        this.dahuabixia = dahuabixia;
    }

    public JLabel getDanjia() {
        return danjia;
    }

    public void setDanjia(JLabel danjia) {
        this.danjia = danjia;
    }

    public JLabel getPrice() {
        return price;
    }

    public void setPrice(JLabel price) {
        this.price = price;
    }

    public int getStateOrNo() {
        return stateOrNo;
    }

    public void setStateOrNo(int stateOrNo) {
        this.stateOrNo = stateOrNo;
    }

    public CBGDahuabiBuyBtn getBuy() {
        return buy;
    }

    public void setBuy(CBGDahuabiBuyBtn buy) {
        this.buy = buy;
    }

    public int getGongshiqiBiaoshi() {
        return gongshiqiBiaoshi;
    }

    public void setGongshiqiBiaoshi(int gongshiqiBiaoshi) {
        this.gongshiqiBiaoshi = gongshiqiBiaoshi;
    }

    public ImageIcon getIcon1() {
        return icon1;
    }

    public void setIcon1(ImageIcon icon1) {
        this.icon1 = icon1;
    }

    public Salegoods getSalegoods() {
        return salegoods;
    }

    public void setSalegoods(Salegoods salegoods) {
        this.salegoods = salegoods;
    }

    public Collection getCollection() {
        return collection;
    }

    public void setCollection(Collection collection) {
        this.collection = collection;
    }

}
